﻿using System;

namespace Validity.Rules.Decorators
{
  public class ConditionalRuleDecorator<TObject, TValue>
      : RuleDecorator<TObject, TValue>
  {
    private readonly Func<TObject, TValue, Boolean> _condition;

    public ConditionalRuleDecorator(IRule<TObject, TValue> rule, Func<TObject, TValue, Boolean> condition)
      : base(rule)
    {
      Verify.NotNull(condition);

      _condition = condition;
    }

    public override Boolean IsViolatedBy(TObject obj, TValue value)
    {
      return _condition.Invoke(obj, value) ? base.IsViolatedBy(obj, value) : false;
    }
  }
}
